package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupWithATTRSVo;
import com.atguigu.gulimall.product.vo.attrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 *    Է   
 *
 * @author wansheng
 * @email 2060054181@gmail.com
 * @date 2024-10-13 19:39:10
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService RelationService;
    @Autowired
    private AttrService attrService;


    @GetMapping("/{catalogId}/withattr")
    public R GerattrGroupWithAttrs(@PathVariable Long catalogId) {
        List<AttrGroupWithATTRSVo> vos=attrGroupService.GetattrGroupWithAttrsBycatalogId(catalogId);
        return R.ok().put("data", vos);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<attrGroupRelationVo> Vos){
        RelationService.saveBatch(Vos);
        return R.ok();
    }


    @GetMapping("/{attrgroupId}/noattr/relation")
    public R AttrNotRelation(@PathVariable Long attrgroupId,@RequestParam Map<String, Object> params){
       PageUtils pageUtils=attrService.getnoRelationAttr(attrgroupId,params);
        return R.ok().put("page",pageUtils);
    }
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody attrGroupRelationVo[] attrGroupRelationVo) {
        attrService.deleteeRelation(attrGroupRelationVo);
        return R.ok();
    }
    @GetMapping("/{attrgroupId}/attr/relation")
    public R AttrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> list=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",list);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPagebyid(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path=categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
